<?PHP
//配送批次控制器
class Admin_sendpi_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','配送批次');
		$this->assign('menuid1','3'); 
		$this->assign('menuid2','1');
	}
	
	/* 配送批次列表 */
	function index__(){
		//权限判断
		factory::module('admin')->authSys('plan');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))			 $_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = ''; 
			if(!isset($_POST['status']))    $_POST['status']  = ''; 
			if(!isset($_POST['b_date']))    $_POST['b_date']  = ''; 
			if(!isset($_POST['e_date']))    $_POST['e_date']  = ''; 
			
			
  	  #查询条件
			$sql_where = ' where   a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.code' 	,'a.senddate' ));  
			if($_POST['status']!=''){
				$sql_where .= ' and a.status= "'.$_POST['status'].'"';	
			}
			if($_POST['b_date']!=''){
				$sql_where .= ' and a.senddate >= "'.$_POST['b_date'].'"';	
			}
			if($_POST['e_date']!=''){
				$sql_where .= ' and a.senddate <= "'.$_POST['e_date'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_sendpi` a  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.*
				from `t_sendpi` a  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}
	}
	 
	# 添加 批次
	public function add__(){
		//权限判断
		factory::module('admin')->authSys('plan');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			
 			$sendvege = array();
			$packet = ''; 
			$db = factory::getDBO(); 
			$db->begin();
			if($db->loadresult('select count(*) from t_sendpi where deleted=0 and code="'.$form['code'].'" ') > 0 ){
				exit('操作失败！,已经存在此配送批次编号！');
			}
			//1 主表
			$sendpi_id = $db->insert('sendpi',array(
				'code'=>$form['code'],
				'senddate'=>$form['senddate'],
				'remark'=>$form['remark'],
				'onsource'=>$form['onsource'],
				'status'=>'0' , 
				'created_user_id'=>factory::module('admin')->getuserid(),
				'created'=>'now()', 
				'updated_user_id'=>factory::module('admin')->getuserid(),
				'updated'=>'now()'
			));
			if(!$sendpi_id){
				$db->rollback();
				exit('系统繁忙，请稍后重试！');
			}
			# 建立溯源信息
			if($form['onsource'] == '1'){
				if(!factory::module('source')->add($sendpi_id)){
					$db->rollback();
					exit('系统繁忙，请稍后重试！');
				}
			}
			$db->commit();
			exit('{success:1,msg:"添加成功！",id:"'.$sendpi_id.'"}');
		} 
	 	$this->assign('nav','配送批次 - 添加配送批次');
	}
	/**
	 * 删除批次
	 */
	public function del__(){
		//权限判断
		factory::module('admin')->authSys('plan');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$id = $_POST['id']; 
			$db = factory::getDBO();
			$rs = $db->loadassoc('select * from t_sendpi where id="'.$id.'"');
			if(!$rs) exit('没有查到任何记录，请重新操作！'); 
			if($rs['status'] != '0'){
				exit('操作失败！只允许删除【计划中】的配送批次！');
			} 
			$db = factory::getDBO(); 
			$db->begin();  
			if(!$db->query('delete from t_sendpi where id="'.$id.'" ')){
				exit('系统繁忙，请稍后再试！');
			}
			if(!$db->query('delete from t_sendfen where sendpi_id="'.$id.'" ')){
				exit('系统繁忙，请稍后再试！');
			}
			if(!$db->query('delete from t_sendfen_vege where sendpi_id="'.$id.'" ')){
				exit('系统繁忙，请稍后再试！');
			}
			if(!$db->query('delete from t_send where sendpi_id="'.$id.'" ')){
				exit('系统繁忙，请稍后再试！');
			}
			if(!$db->query('delete from t_send_vege where sendpi_id="'.$id.'" ')){
				exit('系统繁忙，请稍后再试！');
			}  
			
      if($rs['onsource'] == '1'){
				$rs_source = $db->loadassoc('select * from t_source where sendpi_id="'.$rs['id'].'" ');
				if($rs_source){
					if(!$db->update('source',array(
						'id'=>$rs_source['id'],
						'deleted'=>"1"
					)) ){
						exit('系统繁忙，请稍后再试！');
					}
				}
			}
			
			$db->commit(); 
			exit('{success:1,msg:"操作成功！"}');
		}	
		exit();
	}
	/* 批次修改 */
	public function modify__(){
		//权限判断
		factory::module('admin')->authSys('plan');
		$_GET=format::addslash_deep($_GET);
		$sendpi_id = intval($_GET['id']);
		$db = factory::getDBO();
	  # 配送批次信息
		$rs_sendpi = $db->loadassoc('select a.*,b.username as created_username,c.username as updated_username from `t_sendpi` a left join t_user b on a.created_user_id=b.id left join t_user c on a.updated_user_id=c.id  where a.deleted=0 and a.id="'.$sendpi_id.'" ');
		if(!$rs_sendpi){
			url::msgBox('没有查到任何记录！','-1');
		}
		
		if($rs_sendpi['status']=='2'){
			url::msgBox('不能修改已完成的配送批次！','-1');
		}			
		if($rs_sendpi['status'] == '1'){
			url::msgBox('不能修改生产中的配送批次！','-1');
		} 
		$this->assign('rs_sendpi',$rs_sendpi); 
	}
	/* 批次查看 */
	public function view__(){
		$_GET=format::addslash_deep($_GET);
		$sendpi_id = intval($_GET['id']);
		$db = factory::getDBO();
	  # 配送批次信息
		$rs_sendpi = $db->loadassoc('select a.*,b.username as created_username,c.username as updated_username from `t_sendpi` a left join t_user b on a.created_user_id=b.id left join t_user c on a.updated_user_id=c.id  where a.deleted=0 and a.id="'.$sendpi_id.'" ');
		if(!$rs_sendpi){
			url::msgBox('没有查到任何记录！','-1');
		}
		$this->assign('rs_sendpi',$rs_sendpi); 
	}
	 
  //修改配送批次基本信息
	public function piinfo_modify__(){
		//权限判断
		factory::module('admin')->authSys('plan');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST); 
			$db = factory::getDBO(); 
			if($db->loadresult('select count(*) from t_sendpi where deleted=0 and id<>"'.$_POST['id'].'" and code="'.$_POST['code'].'" ') > 0 ){
				exit('操作失败！,已经存在此配送编号！');
			}
			$rs_sendpi = $db->loadassoc('select * from t_sendpi where id="'.$_POST['id'].'"');
			if($rs_sendpi['status'] != '0'){
				exit('操作失败！只允许修改【计划中】的配送批次基本信息！');
			}
			$db->begin(); 
			$rs_update = array(
				'id'=>$_POST['id'],
				'code'=>$_POST['code'],
				'remark'=>$_POST['remark']
			); 
			//主表
			if(!$db->update('sendpi',$rs_update)){
				exit('操作失败！');
			}
			$db->commit();
			exit('{success:1,msg:"修改成功！"}');
		}
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_sendpi` a where a.deleted=0 and a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1');
		$this->assign('rs',$rs);	
	}
	
	 
	
	//查看某个配送批次的蔬菜包装种类
	public function lookpacket__(){
		$_GET=format::addslash_deep($_GET);
		$sendfen_id = intval($_GET['sendfen_id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_sendfen` a   where a.id="'.$sendfen_id.'" ');
		if(!$rs){
			exit('没有查到任何记录！');
		}	
		$this->assign('rs',$rs);
		
		# 包装个数
		$rs_packet = $db->loadassoclist('select distinct(packet) from t_send where sendfen_id="'.$sendfen_id.'" ');

		foreach($rs_packet as &$v){
			$v['veges'] = factory::module('packet')->decode($v['packet']);
			$v['veges_string'] = factory::module('packet')->decode($v['packet'],'string');
			$v['veges_html'] = factory::module('packet')->decode($v['packet'],'html');
			$v['usernum'] = $db->loadresult('select count(*) from t_send where sendfen_id="'.$sendfen_id.'" and packet="'.$v['packet'].'" ');
		}
		$this->assign('rs_packet',$rs_packet);
	}
	//查看某个配送批次的蔬菜包装种类_某一批会员名称
	public function lookpacket_users__(){

		$_GET=format::addslash_deep($_GET);
		$sendfen_id = intval($_GET['sendfen_id']);
		$packet = $_GET['packet'];
		$db = factory::getDBO();
	
		$users = $db->loadassoclist('select a.user_id,b.username,b.name from `t_send` a left join t_user b on a.user_id=b.id  where a.sendfen_id="'.$sendfen_id.'" and packet="'.$packet.'" ');
		$this->assign('users',$users);
	}
	
	public function lookusers__(){
		$_GET=format::addslash_deep($_GET);
		$sendfen_id = intval($_GET['sendfen_id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_sendfen` a  where a.id="'.$sendfen_id.'" ');
		if(!$rs){
			exit('没有查到任何记录！');
		}
		$this->assign('rs',$rs);
		# 配送会员
		$rs_send = $db->loadassoclist('select a.* ,b.username,b.name,c.val as classes from `t_send` a left join t_user b on a.user_id=b.id left join t_list_user_classes c on b.classes=c.id   where a.sendfen_id="'.$sendfen_id.'" ');
	
		foreach($rs_send as &$v){
			$v['veges'] = factory::module('packet')->decode($v['packet']);
			$v['veges_string'] = factory::module('packet')->decode($v['packet'],'string');
			$v['veges_html'] = factory::module('packet')->decode($v['packet'],'html');
		}
		# 包装个数
		$rs_packet = $db->loadassoclist('select distinct(packet) from t_send where sendpi_id="'.$sendpi_id.'" ');
		$packetindex = array();
		foreach($rs_packet as $k=>&$v){
			$packetindex[$v['packet']] = ($k+1);
		}
		# 包装序列
		$this->assign('packetindex',$packetindex);
		$this->assign('rs_send',$rs_send);
	}
	
	
	
	


 
}
?>